package com.basic.modules.ykf.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.sys.entity.SysFileEntity;
import com.basic.modules.sys.service.SysFileService;
import com.basic.modules.ykf.model.YkfMaterialFileModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfMaterialFileEntity;
import com.basic.modules.ykf.service.YkfMaterialFileService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 素材文件管理
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-05-13 18:21:12
 */
@RestController
@RequestMapping("ykf/materialFile")
public class YkfMaterialFileController {

    @Autowired
    private YkfMaterialFileService ykfMaterialFileService;

    @Autowired
    private SysFileService fileService;

    /**
     * 列表-分页
     */
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfMaterialFileEntity> params){
        PageUtils page = ykfMaterialFileService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfMaterialFileEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getMaterialFileId())) {
            throw new RRException("参数缺失");
        }
        YkfMaterialFileEntity byId = ykfMaterialFileService.getById(params.getMaterialFileId());
        if (ObjectUtil.isNotEmpty(byId)) {
            YkfMaterialFileModel model = new YkfMaterialFileModel();
            BeanUtil.copyProperties(byId, model);
            SysFileEntity file = fileService.getById(byId.getFileId());
            Map<String, Object> map = formatFileSize(file.getFileSize());
            model.setFileSize((BigDecimal) map.get("fileSize"));
            model.setFileSizeUnit((String) map.get("fileSizeUnit"));
            model.setFileType(file.getFileType());
            model.setFileUrl(file.getFileUrl());
            return R.ok(model);
        }
        return R.ok();
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfMaterialFileEntity ykfMaterialFile){
        ValidatorUtils.validateEntity(ykfMaterialFile);
		ykfMaterialFileService.save(ykfMaterialFile);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfMaterialFileEntity ykfMaterialFile){
        ValidatorUtils.validateEntity(ykfMaterialFile);
		ykfMaterialFileService.updateById(ykfMaterialFile);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] materialFileIds){
		ykfMaterialFileService.removeByIds(Arrays.asList(materialFileIds));
        return R.ok();
    }

    public static Map<String, Object> formatFileSize(BigDecimal bytes) {
        Map<String, Object> map = new HashMap<>();
        if (bytes.compareTo(new BigDecimal(1024)) < 0) {
            map.put("fileSize", bytes);
            map.put("fileSizeUnit", "B");
        } else if (bytes.compareTo(new BigDecimal(1024 * 1024)) < 0) {
            map.put("fileSize", bytes.divide(new BigDecimal(1024), 2, RoundingMode.HALF_UP));
            map.put("fileSizeUnit", "K");
        } else if (bytes.compareTo(new BigDecimal(1024 * 1024 * 1024)) < 0) {
            map.put("fileSize", bytes.divide(new BigDecimal(1024 * 1024), 2, RoundingMode.HALF_UP));
            map.put("fileSizeUnit", "M");
        } else {
            map.put("fileSize", bytes.divide(new BigDecimal(1024 * 1024 * 1024), 2, RoundingMode.HALF_UP));
            map.put("fileSizeUnit", "G");
        }
        return map;
    }

}
